package com.gscitysfy.cus.drain.modules.command.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.gscitysfy.cus.drain.modules.command.convert.InstructTeamConvert;
import com.gscitysfy.cus.drain.modules.command.convert.InstructWarehouseConvert;
import com.gscitysfy.cus.drain.modules.command.dto.ResourceDispatchDto;
import com.gscitysfy.cus.drain.modules.command.entity.InstructCar;
import com.gscitysfy.cus.drain.modules.command.entity.InstructTeam;
import com.gscitysfy.cus.drain.modules.command.entity.InstructWarehouse;
import com.gscitysfy.cus.drain.modules.command.mapper.InstructWarehouseMapper;
import com.gscitysfy.cus.drain.modules.command.service.InstructWarehouseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 资源调度 服务实现类
 * </p>
 *
 * @author zhangqiang
 * @since 2022-11-16 19:09:16
 */
@AllArgsConstructor
@Service
public class InstructWarehouseServiceImpl extends ServiceImpl<InstructWarehouseMapper, InstructWarehouse> implements InstructWarehouseService {
    private final InstructWarehouseConvert instructWarehouseConvert;
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public Boolean saveBatch(ResourceDispatchDto dto) {
        if (CollectionUtils.isEmpty(dto.getInstructWarehouseDtoList())) {
            return true;
        }
        List<InstructWarehouse> instructWarehouseList = instructWarehouseConvert.toEntityList(dto);
//        for(InstructTeam instructTeam :instructTeamList ){
//            LambdaQueryWrapper<InstructTeam> queryWrapper = Wrappers.lambdaQuery();
//            queryWrapper.eq(InstructTeam::getTeamId,instructTeam.getTeamId());
//            if(baseMapper.selectCount(queryWrapper)==0){
//                this.save(instructTeam);
//            }
//        }
//        return true;
        return this.saveBatch(instructWarehouseList);
    }

    @Override
    public List<InstructWarehouse> fetchByDangerSituation(Long dangerSituationId) {
        LambdaQueryWrapper<InstructWarehouse> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(InstructWarehouse::getDangerSituationId, dangerSituationId);
        return baseMapper.selectList(queryWrapper);
    }

    @Transactional(rollbackFor = Throwable.class)
    @Override
    public Boolean deleteBatch(List<Long> dangerSituationIdList) {
        LambdaQueryWrapper<InstructWarehouse> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.in(InstructWarehouse::getDangerSituationId, dangerSituationIdList);
        List<InstructWarehouse> instructWarehouseList = baseMapper.selectList(queryWrapper);
        instructWarehouseList.forEach(instructWarehouse -> {
            instructWarehouse.setCmUsable(true);
        });
        return this.updateBatchById(instructWarehouseList);
    }
}
